package de.ubschmidt.capi;

import java.nio.ByteBuffer;

/**
 *
 */
public class DisconnectB3Ind
        extends CapiPassiveMessage
        implements NcciAccessor {

    private static final int OFFSET_NCCI = OFFSET_MESSAGE;
    private static final int OFFSET_REASON = OFFSET_MESSAGE + OFFSET_NCCI;
    private static final int OFFSET_NCPI = OFFSET_REASON + 2;

    public DisconnectB3Ind(final ByteBuffer byteBuffer) {
        super(byteBuffer);
    }

    /** {@inheritDoc} */
    public int getNcci() {
        return getByteBuffer().getInt(OFFSET_NCCI);
    }

    /**
     * Retrieve the {@code Reason} parameter.
     *
     * @return The {@code Reason} parameter.
     */
    public ErrorInfo getReason() {
        ByteBuffer byteBuffer = getByteBuffer();

        if (byteBuffer.limit() > OFFSET_REASON) {
            return ErrorInfo.valueOf(getByteBuffer().getShort(OFFSET_REASON));
        } else {
            return null;
        }
    }

    public CapiStruct getNcpi() {
        ByteBuffer byteBuffer = getByteBuffer();

        if (byteBuffer.limit() > OFFSET_NCPI) {
            byteBuffer.position(OFFSET_NCPI);

            return new CapiStruct(byteBuffer);
        } else {
            return null;
        }
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        String s = String.format(
                "%s[%s ncci=0x%08x reason=%s ncpi=%s]",
                getClass().getSimpleName(),
                super.toString(),
                getNcci(),
                getReason(),
                getNcpi());

        return s;
    }

}
